/**
 * Created by zhangyunqi on 2017/6/13.
 */
import static java.lang.System.out;
import java.sql.*;

public class ConnectionDemo {
    public static void main(String[] args)
            throws ClassNotFoundException, SQLException {
        Class.forName ( "com.mysql.jdbc.Driver" );
        String jdbcUrl = "jdbc:mysql://localhost:3306/world";
        String user = "root" ;
        String passwd = "";
        try (Connection conn =
                     DriverManager.getConnection ( jdbcUrl, user, passwd )) {
            out.printf ( "已%s数据库连接%n", conn.isClosed () ? "关闭" : "打开" );

            Statement stmt = conn.createStatement ();
            String sql="SELECT country.name, city.Name, city.Population FROM country, city WHERE country.Capital = city.id AND country.Continent = 'Asia'";
            ResultSet rs = stmt.executeQuery(sql);
            System.out.println(  "国家名字" +"\t"+ "\t"+ "首都城市名字"+"\t"+ "\t"+ "城市人口");
            int count = 0;
            while (rs.next ()) {

                System.out.print ( rs.getString ( 1 ) + "\t" + "\t" );
                System.out.print ( rs.getString ( 2 ) + "\t" + "\t" );
                System.out.print ( rs.getInt ( 3 ) + "\t" + "\t" );
                System.out.println ();
                count++;
            }
            if (rs.next ()) ;


            rs.close ();
            stmt.close ();
            conn.close ();

        } catch (SQLException e) {
            e.printStackTrace ();
        }

    }
}
